package cn.com.ddapp.ddm.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.mine.BankList;


/**
 * User: Administrator
 * Date: 2016/7/30 0030
 * Time: 10:09
 */
public class SpinnerPopView<T> extends PopupWindow{
     //private List<T> list;
     private List<BankList.ListBean> list;
     private Context context;
     LayoutInflater inflater;
     private ListView listView;
    private MSpinnerAdapter adapter;

    /*public SpinnerPopView(Context context, List<T> list, AdapterView.OnItemClickListener clickListener) {
        super(context);
        inflater=LayoutInflater.from(context);
        this.list=list;
        init(clickListener);
    }*/
    public SpinnerPopView(Context context, List<BankList.ListBean> list, AdapterView.OnItemClickListener clickListener) {
        super(context);
        inflater=LayoutInflater.from(context);
        this.list=list;
        init(clickListener);
    }

    private void init(AdapterView.OnItemClickListener clickListener) {
        View view=inflater.inflate(R.layout.spiner_window_layout,null);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw=new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        listView= (ListView) view.findViewById(R.id.spinner_listview);
        adapter=new MSpinnerAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(clickListener);
    }

    public class MSpinnerAdapter extends BaseAdapter{



        @Override
        public int getCount() {
            return list == null ?0 :list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if(view == null){
                view=inflater.inflate(R.layout.toshow_spinner,null);
                viewHolder=new ViewHolder();
                viewHolder.tv= (TextView) view.findViewById(R.id.spinner_tv);
                view.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) view.getTag();
            }
            viewHolder.tv.setText(list.get(i).getName());
            return view;
        }

        class ViewHolder{
            TextView tv;
        }
    }
}
